package com.leyou.item.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.servcie.BrandService;
import com.leyou.pojo.dto.BrandDto;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 根据品牌ids查询品牌的集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDto>> queryBrandById(@RequestParam("ids") List<Long> ids){
        List<Brand> brandList = brandService.listByIds(ids);
        List<BrandDto> brandDtoList = brandList.stream().map(BrandDto::new).collect(Collectors.toList());
        return ResponseEntity.ok(brandDtoList);
    }

    /**
     * 根据分类id查询品牌
     * @param categoryId
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDto>>  queryBrandByCategoryId(@RequestParam("id") Long categoryId){
       List<BrandDto> brandDtoList = brandService.queryBrandByCategoryId(categoryId);
       return ResponseEntity.ok(brandDtoList);
    }

    /**
     * 分页查询品牌
     * @param key
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDto>> queryBrandByPage(@RequestParam(value = "key",required = false) String key,
                                                              @RequestParam(value = "page",defaultValue ="1") Integer page,
                                                              @RequestParam(value = "rows",defaultValue = "5") Integer rows){

     PageDTO<BrandDto> brandDtoPageDTO =  brandService.queryBrandByPage(key,page,rows);
        return ResponseEntity.ok(brandDtoPageDTO);

    }

    /**
     * 根据品牌id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDto>  queryBrandById(@PathVariable("id") Long id){
        Brand brand = brandService.getById(id);
        return ResponseEntity.ok(new BrandDto(brand));
    }

    /**
     * 新增品牌
     *
     * @param brandDto
     * @return
     */
    @PostMapping
    public ResponseEntity<Void>  saveBrand(BrandDto brandDto){
        brandService.saveBrand(brandDto);
        return ResponseEntity.status(201).build();
    }

    /**
     * 更新品牌
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDto brandDto){
        brandService.updateBrand(brandDto);
        return ResponseEntity.status(204).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long brandId){
        brandService.deleteBrand(brandId);
        return ResponseEntity.status(204).build();
    }
}
